using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Helper;
using Ent.Web.WebControl;
using Ent.Web.WebControl.Parameters;
using Ent.Voice.Show;
public partial class Controls_Event_AdminSettings : Ent.Web.WebControl.UserControl
{
    private Settings _pop;
    int minVoteCount = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        object maxVoteCount = GetASPXParam("maxVoteCount");
        rvMaxVoteCount.MaximumValue = maxVoteCount != null ? maxVoteCount.ToString() : "";
        rvMaxVoteCount.MinimumValue = minVoteCount.ToString();
        Dictionary<string, string> rep = new Dictionary<string, string>();
        rep.Add("$1", rvMaxVoteCount.MinimumValue);
        rep.Add("$2", rvMaxVoteCount.MaximumValue);
        rep.Add("$3", "Max Vote Count");
        rvMaxVoteCount.ErrorMessage = ResourceHelper.Get("MSG_RVMaxCount", rep);

        rep = new Dictionary<string, string>();
        rep.Add("$1", "Max Vote Count");
        rep.Add("$2", "Max Vote Count");
        rfvMaxVoteCount.ErrorMessage = ResourceHelper.Get("MSG_RFVMaxCount", rep);

        rvDueDate.MaximumValue = DateTime.Today.AddMonths(Ent.Web.Helper.MathFunction.ConvertToInteger(GetASPXParam("maxDuration"), 2)).ToShortDateString();
        rvDueDate.MinimumValue = DateTime.Today.ToShortDateString();
        rep = new Dictionary<string, string>();
        rep.Add("$1", rvDueDate.MinimumValue);
        rep.Add("$2", rvDueDate.MaximumValue);
        rep.Add("$3", "Event End");
        rvDueDate.ErrorMessage = ResourceHelper.Get("MSG_RVDueDate", rep);

        rfvDueDate.ErrorMessage = ResourceHelper.Get("MSG_RFVDueDate");
        
        rfvAccessCode.ErrorMessage = ResourceHelper.Get("MSG_RFVAccessCode");
        rfvTitle.ErrorMessage = ResourceHelper.Get("MSG_RFVTitle");
                                            
        lblAccessCode.Text = ResourceHelper.Get("LBL_Access");
        lblAccessibility.Text = ResourceHelper.Get("LBL_Accessibility");
        lblCatagory.Text = ResourceHelper.Get("LBL_Catagory");
        lblDescription.Text = ResourceHelper.Get("LBL_Description");
        lblEndOn.Text = ResourceHelper.Get("LBL_EndOn");
        lblTitle.Text = ResourceHelper.Get("LBL_Title");

        btnShowCal.Attributes.Add("onclick", "show_cal(400, 160, this, '" + txtDueDate.ClientID + "');");
        PopuplateControls();
    }
    public override void AddJavascript()
    {
        try
        {
            LoadJavascript("Javascripts/Settings.js");
            LoadJavascript("Javascripts/calendar.js");
            divJavascript.InnerHtml += JavascriptText;
        }
        catch (Exception exc)
        { }
    }
    public void PopuplateControls()
    {
        _pop = new Settings(Server.MapPath("Data/Settings.xml"));
        ddlCatagory.Items.AddRange(_pop.GetItemCollection("Catagory"));
        rblAccess.Items.AddRange(_pop.GetItemCollection("Accessibility"));
        cblAccessPublic.Items.AddRange(_pop.GetItemCollection("AccessPublic"));
    }
    public override void SetEvents()
    {

        if (rblAccess.SelectedIndex == 1)
        {
            OnLoadDisable(new string[] { txtAccessCode.ClientID });
            OnLoadExecute(new string[] { "_ValidatorEnable(document.getElementById('" + rfvAccessCode.ClientID + "'),false)" });
        }
        rblAccess.Attributes.Add("onclick", "javascript:var isPrivate=document.getElementById('" + rblAccess.ClientID + "_0').checked && !document.getElementById('" + rblAccess.ClientID + "_0').disabled; SetDisabled(document.getElementById('" + txtAccessCode.ClientID + "'),!isPrivate); SetDisabled(document.getElementById('" + cblAccessPublic.ClientID + "'),isPrivate); _ValidatorEnable(document.getElementById('" + rfvAccessCode.ClientID + "'),isPrivate);");
        if (rblAccess.SelectedIndex == 0)
        {
            OnLoadDisable(new string[] { cblAccessPublic.ClientID });
            OnLoadEnable(new string[] { txtAccessCode.ClientID });
            OnLoadExecute(new string[] { "_ValidatorEnable(document.getElementById('" + rfvAccessCode.ClientID + "'),true)" });
        }
        rbFixed.Attributes.Add("onclick", "javascript:if(document.getElementById('" + rbFixed.ClientID + "').checked && !document.getElementById('" + rbFixed.ClientID + "').disabled) " +
            "{ SetDisabled(document.getElementById('" + txtMaxVoteCount.ClientID + "'),true); " +
            "SetDisabled(document.getElementById('tdFixed'),false); " +
            "_ValidatorEnable(document.getElementById('" + rfvDueDate.ClientID + "'),true); " +
            "_ValidatorEnable(document.getElementById('" + rvDueDate.ClientID + "'),true); " +
            "_ValidatorEnable(document.getElementById('" + rfvMaxVoteCount.ClientID + "'),false); " +
            "_ValidatorEnable(document.getElementById('" + rvMaxVoteCount.ClientID + "'),false); " +
            "}");
        if (rbFixed.Checked)
        {
            OnLoadDisable(new string[] { txtMaxVoteCount.ClientID });
            OnLoadExecute(new string[] { 
                    "SetDisabled(document.getElementById('" + txtDueDate.ClientID + "'), false)", 
                    "SetDisabled(document.getElementById('" + txtMaxVoteCount.ClientID + "'), true)", 
                    "_ValidatorEnable(document.getElementById('" + rfvDueDate.ClientID + "'),true)", 
                    "_ValidatorEnable(document.getElementById('" + rvDueDate.ClientID + "'),true)",
                    "_ValidatorEnable(document.getElementById('" + rfvMaxVoteCount.ClientID + "'),false)",
                    "_ValidatorEnable(document.getElementById('" + rvMaxVoteCount.ClientID + "'),false)"});
        }
        rbCount.Attributes.Add("onclick", "javascript:if(document.getElementById('" + rbCount.ClientID + "').checked && !document.getElementById('" + rbCount.ClientID + "').disabled) " +
            "{ SetDisabled(document.getElementById('" + txtMaxVoteCount.ClientID + "'), false); " +
            "SetDisabled(document.getElementById('tdFixed'),true); " +
            "_ValidatorEnable(document.getElementById('" + rfvDueDate.ClientID + "'),false); " +
            "_ValidatorEnable(document.getElementById('" + rvDueDate.ClientID + "'),false); " +
            "_ValidatorEnable(document.getElementById('" + rfvMaxVoteCount.ClientID + "'),true); " +
            "_ValidatorEnable(document.getElementById('" + rvMaxVoteCount.ClientID + "'),true); " +
            "}");
        if (rbCount.Checked)
        {
            OnLoadDisable(new string[] { "tdFixed" });
            OnLoadExecute(new string[] { 
                    "SetDisabled(document.getElementById('" + txtDueDate.ClientID + "'), true)", 
                    "SetDisabled(document.getElementById('" + txtMaxVoteCount.ClientID + "'), false)", 
                    "_ValidatorEnable(document.getElementById('" + rfvDueDate.ClientID + "'),false)", 
                    "_ValidatorEnable(document.getElementById('" + rvDueDate.ClientID + "'),false)",
                    "_ValidatorEnable(document.getElementById('" + rfvMaxVoteCount.ClientID + "'),true)",
                    "_ValidatorEnable(document.getElementById('" + rvMaxVoteCount.ClientID + "'),true)"});
        }
    }
    public override void Init()
    {
        foreach (string key in Values.Keys)
        {
            if (Values[key] == null)
                continue;
            switch (key)
            {
                #region Primary data : 3
                case EventProperty.Catagory:
                    ddlCatagory.SelectedIndex = ddlCatagory.Items.IndexOf(ddlCatagory.Items.FindByValue(Values[key].ToString()));
                    break;
                case EventProperty.Title:
                    txtTitle.Text = (string)Values[key];
                    break;
                case EventProperty.Description:
                    txtDescription.Text = (string)Values[key];
                    break;
                #endregion
                #region Accessibility : 2
                case EventProperty.Access:
                    SetCollectionControl(rblAccess, (string[])Values[key]);
                    break;
                case EventProperty.AccessPublic:
                    SetCollectionControl(cblAccessPublic, (string[])Values[key]);
                    break;
                case EventProperty.AccessCode:
                    txtAccessCode.Text = (string)Values[key];
                    break;
                #endregion
                #region End Date : 5
                case EventProperty.Fixed:
                    rbFixed.Checked = Convert.ToBoolean(Values[key]);
                    break;
                case EventProperty.Count:
                    rbCount.Checked = Convert.ToBoolean(Values[key]);
                    break;
                case EventProperty.StartDate:
                    rvDueDate.MaximumValue = DateTime.Parse(Values[key].ToString()).AddMonths(Ent.Web.Helper.MathFunction.ConvertToInteger(GetASPXParam("maxDuration"), 2)).ToShortDateString();
                    Dictionary<string, string> rep = new Dictionary<string, string>();
                    rep.Add("$1", rvDueDate.MinimumValue);
                    rep.Add("$2", rvDueDate.MaximumValue);
                    rep.Add("$3", "Event End");
                    rvDueDate.ErrorMessage = ResourceHelper.Get("MSG_RVDueDate", rep);
                    break;
                case EventProperty.DueDate:
                    if (!Values[key].ToString().Equals(Event.MaxDate.ToShortDateString()))
                        txtDueDate.Text = Values[key].ToString();
                    break;
                case EventProperty.MaxVoteCount:
                    if (!Values[key].ToString().Equals("-1"))
                        txtMaxVoteCount.Text = Values[key].ToString();
                    break;
                    #endregion
                default:
                    break;
            }
        }
    }
    public override void Dispose()
    {
        //throw new Exception("The method or operation is not implemented.");
    }
    public override Dictionary<string, object> GetValues()
    {
        Dictionary<string, object> retValue = new Dictionary<string, object>();
        #region Primary Data : 3
        retValue.Add(EventProperty.Catagory, ddlCatagory.SelectedValue);
        retValue.Add(EventProperty.Title, txtTitle.Text);
        retValue.Add(EventProperty.Description, HttpUtility.HtmlEncode(txtDescription.Text));
        #endregion
        #region End Date : 5
        retValue.Add(EventProperty.Count, rbCount.Checked);
        retValue.Add(EventProperty.MaxVoteCount, txtMaxVoteCount.Text);
        retValue.Add(EventProperty.Fixed, rbFixed.Checked);
        retValue.Add(EventProperty.StartDate, (Values.ContainsKey(EventProperty.StartDate) ? Values[EventProperty.StartDate].ToString() : DateTime.Today.ToShortDateString()));
        DateTime duedate = Event.MaxDate;
        if (rbFixed.Checked)
            DateTime.TryParse(txtDueDate.Text, out duedate);
        retValue.Add(EventProperty.DueDate, duedate.ToShortDateString());
        #endregion
        #region Accessibility : 2
        retValue.Add(EventProperty.Access, Ent.Web.WebControl.Helper.GetSelectedValues(rblAccess.Items));
        retValue.Add(EventProperty.AccessPublic, Ent.Web.WebControl.Helper.GetSelectedValues(cblAccessPublic.Items));
        retValue.Add(EventProperty.AccessCode, txtAccessCode.Text);
        #endregion
        return retValue;
    }
}
